//
//  UserAccount.swift
//  01-界面搭建
//
//  Created by Apple on 15/11/6.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {
    /*
    
    "access_token" = "2.00ml8IrF6dP8NE6791982000uQBsmB";
    "expires_in" = 157679999;
    "remind_in" = 157679999;
    uid = 5365823342;
    
    */
    //MARK: - 用户模型
    /// 用于调用access_token，接口获取授权后的access token
    var access_token:String?
    /// access_token的生命周期，单位是秒数 - 准确的数据类型是`数值`
    var expires_in:NSTimeInterval = 0 {
        didSet{
            /// 一旦从服务器获得过期的时间，立刻计算准确的日期
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
            
        }
        
    }
    
    /// 过期日期
    var expiresDate:NSDate?
    /// 当前授权用户的UID
    var uid:String?
    /// 用户昵称
    var screen_name: String?
    /// 用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    //重载构造方法 字典转模型
    init(dict:[String:AnyObject]) {
        //创建对象
        super.init()
        setValuesForKeysWithDictionary(dict)
        //        expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    //描述对象的信息
    override var description: String {
        let keys = ["access_token","expires_in", "uid", "expiresDate","screen_name","avatar_large"]
        return "\(dictionaryWithValuesForKeys(keys))"
    }

    //MARK: - 归解档
    //对象方法：保存的是对象的属性
    ///归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    ///解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
    }
}
